package netty.learning.demo.application;

import java.util.HashMap;

public class ReflectionJs {
    private static final String COMMAND_SPLIT_STRING = "。";
    private static final String WORD_SPLIT_STRING = " ";
    private static final String PUT_ACTION_WORD = "对";
    private static final String ACTION_INVOKER = "动作发起者";
    private static final String ACTION_PARTICIPANT = "动作参与者";
    private static final String ACTION_NAME = "动作名称";
    private static final String ACTION_PARAM = "参数";

    private static String[] explainContent(String content) {
        String[] commandArray  = content.split(COMMAND_SPLIT_STRING);
        return commandArray;
    }


    private static HashMap<String, Object> explainCommand(String command) {
        HashMap<String, Object> actionMap = new HashMap<>();
        actionMap.put("原句", command);
        if(null == command || command.length() == 0){
            return actionMap;
        }
        String[] wordArray  = command.split(WORD_SPLIT_STRING);
        int wordArraySize  = wordArray.length;
        if(wordArraySize < 2){
            return actionMap;
        }

        if(PUT_ACTION_WORD.equals(wordArray[1])){
            if(wordArraySize < 4){
                return actionMap;
            }
            actionMap.put(ACTION_INVOKER, wordArray[0]);
            actionMap.put(ACTION_PARTICIPANT, wordArray[2]);
            if(wordArraySize == 4){
                actionMap.put(ACTION_NAME, wordArray[3]);
            }else{
                String actionName = wordArray[3]+wordArray[wordArraySize-1];
                actionMap.put(ACTION_NAME, actionName);
                actionMap.put(ACTION_PARAM, wordArray[4]);
            }

            return actionMap;
        }
        actionMap.put(ACTION_INVOKER, wordArray[0]);

        if(wordArraySize == 2) {
            actionMap.put(ACTION_NAME, wordArray[1]);
        }
        else if(isSelfActionWord(wordArray[1])){
            if(wordArraySize==2){
                actionMap.put(ACTION_NAME, wordArray[1]);
            }else{
                String actionName = wordArray[1]+wordArray[wordArraySize-1];
                actionMap.put(ACTION_NAME, actionName);
                actionMap.put(ACTION_PARAM, wordArray[2]);
            }

        }else{
            actionMap.put(ACTION_PARTICIPANT, wordArray[2]);
            if(wordArraySize == 3){
                actionMap.put(ACTION_NAME, wordArray[1]);
            }else{
                String actionName = wordArray[1]+wordArray[wordArraySize-1];
                actionMap.put(ACTION_NAME, actionName);
                actionMap.put(ACTION_PARAM, wordArray[3]);
            }
        }

        return actionMap;
    }

    private static boolean isSelfActionWord(String s) {
        return "摸".equals(s);
    }

    private static void executeActionMap(HashMap<String, Object> actionMap){
        if(actionMap.containsKey(ACTION_PARTICIPANT)){
            if(actionMap.containsKey(ACTION_PARAM)){
                System.out.println("a(String,q,int)");
            }else{
                System.out.println("a(String,q)");
            }
        }else{
            if(actionMap.containsKey(ACTION_PARAM)){
                System.out.println("a(String,int)");
            }else{
                System.out.println("k(String)");
            }

        }
    }

    public static void executeContent(String content){
        String[] commandArray = explainContent(content);
        for(String command : commandArray){
            HashMap<String, Object> actionMap = explainCommand(command);
            System.out.println(command+"="+actionMap);
            executeActionMap(actionMap);
        }
    }


    public static void main(String[] args) {
        String content = "角色1 对 角色2 造成 1点 伤害。角色1 摸 2张 牌。角色1 制衡";
        //executeContent(content);
        HashMap<String, Object> contextMap = new HashMap<>();
        contextMap.put("key","fff");
        String fff = (String)contextMap.get("key");
        System.out.println(fff);
    }

}
